package com.ilibrary.web.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.service.NewsService;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.NewsVO;

public class DoQueryNewsListAction extends MyAjaxAction {

	private NewsService newsService;
	
	public NewsService getNewsService() {
		return newsService;
	}
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		
		String newsCategoryId = request.getParameter("newsCategoryId");
		String searchCondition = request.getParameter("searchCondition");
		
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject joo = null;
		
		ResultDto<NewsVO> resultDto = newsService.queryList(start, limit, newsCategoryId, searchCondition);
		List<NewsVO> newsVOList = resultDto.getResultList();
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		for (NewsVO newsVO : newsVOList) {
			joo = new JSONObject();
			joo.put("newsId", newsVO.getNewsId());
			joo.put("newsTitle", newsVO.getNewsTitle());
			joo.put("newsContent", newsVO.getNewsContent());
			joo.put("newsPageviews", newsVO.getNewsPageviews());
			joo.put("newsCategoryId", newsVO.getNewsCategoryId());
			joo.put("newsCategoryName", newsVO.getNewsCategoryName());
			joo.put("createDt", df.format(newsVO.getCreateDt()));
			ja.add(joo);
		}
		jo.put("data", ja);
		jo.put("total", resultDto.getTotal());
		return jo;
	}

}
